package com.nian.search.es.controller;

import com.nian.common.model.ServerResponse;
import com.nian.search.es.model.Item;
import com.nian.search.es.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 用途：
 * 开发：zhangmj
 * 日期：2020/4/11 0011 17:03
 */
@RestController
@RequestMapping("es/test")
public class ESSimpleController {

    @Autowired
    private IItemService iItemService;

    @PostMapping("get/{id}")
    public ServerResponse getInfo(@PathVariable("id") Long id) {
        return ServerResponse.createBySuccess(iItemService.getById(id));
    }

    @PostMapping("search")
    public ServerResponse search(String query) {
        Page<Item> list = iItemService.search(query);
        return ServerResponse.createBySuccess(list);
    }

    @PostMapping("search/title")
    public ServerResponse findByTile(String title) {
        return ServerResponse.createBySuccess(iItemService.findByTile(title));
    }

    @PostMapping("add")
    public ServerResponse add(@RequestBody Item model) {
        Item item = iItemService.add(model);
        return ServerResponse.createBySuccess(item);
    }

    @PostMapping("addTest")
    public ServerResponse addTest() {
        List<Item> list = new ArrayList<>();
        list.add(new Item(1L, "小米手机7", "手机", "小米", 1999.00, "http://l27.0.0.1/10001.jpg"));
        list.add(new Item(2L, "坚果手机R1", "手机", "锤子", 1999.00, "http://l27.0.0.1/10002.jpg"));
        list.add(new Item(3L, "华为META10", "手机", "华为", 1299.00, "http://l27.0.0.1/10003.jpg"));
        list.add(new Item(4L, "小米Mix2S", "手机", "小米", 1099.00, "http://l27.0.0.1/10004.jpg"));
        list.add(new Item(5L, "荣耀V10", "手机", "华为", 1199.00, "http://l27.0.0.1/10005.jpg"));
        return ServerResponse.createBySuccess(iItemService.saveAll(list));
    }
}
